package com.gowhere.controller.system;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.gowhere.controller.BaseServlet;
import com.gowhere.domain.Permission;
import com.gowhere.domain.Role;
import com.gowhere.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/system/role/*")
public class RoleServlet extends BaseServlet {
    //查询操作
    protected void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //页面获取当前页码和每业下是条目数
        int pageNum = 1;
        int pageSize = 3;
        //获取页面传递的当前页码

        String pageNumStr = req.getParameter("page");
        String pageSizeStr = req.getParameter("pageSize");
        //判断前端传递过来的数据是否为空或null
        if (StringUtils.isNotBlank(pageNumStr)) {//不为空,不为null
            pageNum = Integer.parseInt(pageNumStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        //调用业务层的findByPage方法,并把页面参数传递过去
        List<Role> list = roleService.findByPage(pageNum, pageSize);
        PageInfo<Role> pageInfo = new PageInfo<>(list);
        //将req存入作用域

        req.setAttribute("page", pageInfo);
        //跳转到list页面
        req.getRequestDispatcher("/pages/system/role-list.jsp").forward(req, resp);
    }

    //添加操作
    protected void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端页面要添加的数据
        Role role = BeanUtil.fillBean(req, Role.class);
        //调用方法
        roleService.save(role);
        //跳转页面
        req.getRequestDispatcher("/system/role/findByPage").forward(req, resp);
    }

    //删除操作
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端要删除的id数组
        String ids = req.getParameter("ids");
        //调用service的删除方法
        roleService.delete(ids);
        //跳转回遍历页面
        req.getRequestDispatcher("/system/role/findByPage").forward(req, resp);
    }

    //根据id查询,展示数据
    protected void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端要展示数据的id
        String id = req.getParameter("id");

        //调用业务层的查询方法
        Role role = roleService.findById(id);
        //将获取到的数据提交给前端
        req.setAttribute("role", role);
        //返回详情页面
        req.getRequestDispatcher("/pages/system/role-show.jsp").forward(req, resp);
    }


    protected void findRolePmByRid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //获取前端页面id
        String roleId = req.getParameter("id");
        //获取对应名称
        String roleName = req.getParameter("roleName");
        String rn = new String(roleName.getBytes("ISO-8859-1"), "UTF-8");
        //调用业务层findRolePmByRid方法,获取到集合
        List<Map> list = roleService.findRolePmByRid(roleId);
        //经获取到的数据传回前端
        req.setAttribute("roleId", roleId);
        req.setAttribute("roleName", rn);
        ObjectMapper om = new ObjectMapper();
        String ad = om.writeValueAsString(list);
        req.setAttribute("permissionData", ad);
        req.getRequestDispatcher("/pages/system/role-permission-add.jsp").forward(req, resp);
    }

    //修改权限
    protected void updateRolePm(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roleId = req.getParameter("roleId");
        String permissions = req.getParameter("permissions");
        System.out.println(roleId);
        System.out.println(permissions);
        roleService.updateRolePm(roleId, permissions);
        findByPage(req, resp);

    }
}
